WIN32_PROGRAM = SipTestTool.exe
UNIX_PROGRAM = SipTestTool

SOURCE := $(wildcard $(patsubst %,%/*.cpp,src)) 
OBJECTS = $(patsubst %.cpp,%.o,$(SOURCE))
dependencies = 	$(subst .o,.d,$(OBJECTS))

ifndef WXCFG
	WXCFG=wx-config
endif

ifndef NOTDEBUG
	CXXFLAGS+=  -g 
endif

#Osip=libosip2-2.2.2
Osip=libosip2

# implementation

.SUFFIXES:	.o .cpp

all:    $(UNIX_PROGRAM) daemon caseeditor
both: win32 $(UNIX_PROGRAM) docs

Debug: all

caseeditor:
	$(MAKE) -C tools/caseeditor

docs:
	$(MAKE) -C doc

CXXFLAGS+= -I/usr/include/osip2-2.2.2  -Isrc `xml2-config --cflags` `$(WXCFG) --cxxflags` `pkg-config --cflags $(Osip)`  -Wall
LIBS+=`$(WXCFG) --libs`  `xml2-config --libs` `pkg-config --libs $(Osip)`

BUILDTAG= `date '+%Y-%m-%d %H:%M'`

VERSION=`grep -o -E "[0-9]{1,}.[0-9]{2,}" SipTestTool/src/version.h`

daemon:
	make -C tools/daemon

daemon.exe:
	make -C tools/daemon daemon.exe

ifneq "$(MAKECMDGOALS)" "clean"
  -include $(dependencies)
endif

define make-depend
	@$(CXX) $(CXXFLAGS) -MM -MF $(subst .o,.d,$@)  -MP -MT $2  $1
endef

%.o: %.cpp
	$(call make-depend,$<,$@)
	$(CXX) -c $(CXXFLAGS)  -o  $@ $<

$(UNIX_PROGRAM): $(OBJECTS)
	@echo "#include <string>" >BuildTag.cpp
	@echo "std::string BuildTag=\"$(BUILDTAG)\";">>BuildTag.cpp
	$(CXX) -c BuildTag.cpp -o BuildTag.o
	$(CXX) -o $(UNIX_PROGRAM) $(OBJECTS) BuildTag.o  $(LIBS)
	rm -f BuildTag.*


$(WIN32_PROGRAM):$(OBJECTS)  SipTestToolRes.o
	@echo "#include <string>" >BuildTag.cpp
	@echo "std::string BuildTag=\"$(BUILDTAG)\";">>BuildTag.cpp
	@echo document.write\(\"$(BUILDTAG)\"\)\; >JsBuildTag.js 
	$(CXX) -c BuildTag.cpp -o BuildTag.o
	$(CXX) -o $(WIN32_PROGRAM) $(OBJECTS) $(LIBS) BuildTag.o  SipTestToolRes.o
	rm -f BuildTag.*

SipTestToolRes.o: SipTestTool.rc
	i386-mingw32msvc-windres -i SipTestTool.rc -o SipTestToolRes.o --include-dir D:/wxWindows-2.4.2/include --define __WIN32__ --define __WIN95__ --define __GNUWIN32__

clean: 
	rm -f *.o  $(OBJECTS) SipTestToolRes.o $(dependencies)

distclean: clean
	rm -f $(WIN32_PROGRAM) $(UNIX_PROGRAM) CaseEditor DaemonStt
	make -C tools/daemon clean
	make -C tools/caseeditor clean

win32dist:
	cd .. && tar jcvf SipTestTool/stt_win32_bin_${VERSION}.tbz2 SipTestTool/*.exe SipTestTool/*.png  SipTestTool/zh_CN/*.mo SipTestTool/default.xml SipTestTool/scenarios/case*.xml --exclude "*Debug.exe"

dlldist:
	tar jcvf stt_win32_dlls.tbz2 *.dll

source:
	cd .. && tar jcvf SipTestTool/stt_${VERSION}.tbz2 SipTestTool --exclude "*.o"  --exclude "*.svn/*" --exclude "*.exe" --exclude "*.dll" --exclude "*.js" --exclude "*.tbz2" --exclude "CaseEditor" --exclude "SipTestTool/SipTestTool" --exclude "sttdaemon" --exclude "tags" --exclude "*.d" --exclude "*.swp" --exclude "*.log" --exclude "*report*" --exclude "*.svn" --exclude "*~" --exclude "*/sav*" --exclude "*.html" --exclude "TAGS" --exclude "*/cgi*" --exclude "*/siptesttool.wiki*"

linuxdist:
	tar zcvf SipTestTool.tgz SipTestTool zh_CN/*.mo scenarios/*.xml default.xml

install: 
	install -d $(DESTDIR)/opt/siptesttool
	install -m 777  -d $(DESTDIR)/opt/siptesttool/scenarios
	install -m 777  -d $(DESTDIR)/opt/siptesttool/report 
	install -d $(DESTDIR)/opt/siptesttool/zh_CN
	install ./SipTestTool ./DaemonStt ./CaseEditor ./default.xml ./splash.png  $(DESTDIR)/opt/siptesttool
	install ./zh_CN/*.mo ./zh_CN/*.po   $(DESTDIR)/opt/siptesttool/zh_CN
	install ./scenarios/*.xml  $(DESTDIR)/opt/siptesttool/scenarios
win32ubuntu:
	make -C tools/caseeditor win32ubuntu
	make -C tools/daemon win32ubuntu
	bash win32rel.sh.ubuntu
win32:
	make -C tools/caseeditor win32
	make -C tools/daemon win32
	bash win32rel.sh noclean
	cp SipTestTool.exe SipTestTool-Debug.exe
	/opt/xmingw/bin/i386-mingw32msvc-strip SipTestTool.exe	
